void stretch()
{
  if(ori)
  {
    if(width < scr_h && height < scr_w) return;
    if(width>height)
    {
      swidth=scr_h;
      sheight=scr_h*100/width*height/100;
      pozx=0;
      pozy=(scr_w-sheight)/2;
      if(pozy<0) goto sq;
    }else{
    sq:
      sheight=scr_w;
      swidth=scr_w*100/height*width/100;
      pozx=(scr_h-swidth)/2;
      pozy=0;
    }
  }else{
    if(width < scr_w && height < scr_h) return;
    if(width>height)
    {
      swidth=scr_w;
      sheight=scr_w*100/width*height/100;
      pozx=0;
      pozy=(scr_h-sheight)/2;
    }else{
      sheight=scr_h;
      swidth=scr_h*100/height*width/100;
      pozx=(scr_w-swidth)/2;
      pozy=0;
    }
  }
}

void center(void)
{
  if(ori)
  {
    pozx=(scr_h-swidth)/2;    
    pozy=(scr_w-sheight)/2;
  }else{
    pozx=(scr_w-swidth)/2;
    pozy=(scr_h-sheight)/2;
  }
}

void real_size(void)
{
  sheight=height;
  swidth=width;
  center();
}

void unreg(void)
{
  char error_code;
  REQUEST_IMAGEHANDLER_INTERNAL_UNREGISTER(SYNC,image[0].ImageHandle,0,0,image[0].ImageID,1,&error_code);
}

u16 RegisterImage(wchar_t * path, wchar_t * fname)
{
  char error_code;
  image->ImageID=0xFFFF;
  image->ImageHandle=0xFFFF;
  if (!fstat(path,fname,0))
  {
    if (!REQUEST_IMAGEHANDLER_INTERNAL_GETHANDLE(SYNC,&image->ImageHandle,&error_code))
    {
      if (!REQUEST_IMAGEHANDLER_INTERNAL_REGISTER(SYNC,image->ImageHandle,path,fname,0,&image->ImageID,&error_code))
      {
        if (error_code)
        {
          image->ImageHandle=0xFFFF;
        }
      }
    }
  }
  width  = GetImageWidth(image->ImageID);
  height = GetImageHeight(image->ImageID);
  if(autoorient)
  {
    if(width>height && width>scr_w) ori=1;
    else if(height>width && height>scr_w) ori=0;
  }
  center();
  real_size();
  if(autostretch) stretch();
  return(image->ImageID);
};

void list (void)
{
  MMSBook->flist=List_New();
  int xp=0;
  void *diry=w_diropen(MMSBook->path);
  if (diry)
  {
    wchar_t *next;
    w_chdir(MMSBook->path);
    while(next=w_dirread(diry))
    {
      if(wstrcmp(next,MMSBook->fname)==0) xpp=xp;
      FILE *f=new FILE;
      f->strname=Str2ID(next,0,SID_ANY_LEN);  //name[xp]=Str2ID(next,0,SID_ANY_LEN);
      if(ListElement_GetByIndex(MMSBook->flist,xp)) ListElement_Remove(MMSBook->flist,xp);
      ListElement_Insert(MMSBook->flist,xp,f);
      if(wstrwstr(next,L".")) xp++;
    }
    w_dirclose(diry);
  }
  if(xp)ilo=xp-1;
}

void BoardGuiOnRedraw(DISP_OBJ_board *db,int ,int,int)
{
  STRID str;
  RECT rc_old;
  void *gc=get_DisplayGC();
  get_GC_RECT(gc, &rc_old);
  DrawRect(0,0,Display_GetWidth(0),Display_GetHeight(0),bg_cl,bg_cl);
  putchar(gc, pozx, pozy, swidth, sheight, image[0].ImageID); // <<< draw
  wchar_t text[40];
  SetFont(FONT);
  int heightt = GetImageHeight(' ');
  if(info)
  {
    DrawString(((FILE*)ListElement_GetByIndex(MMSBook->flist,xpp))->strname,0, i_X_POS,i_Y_POS,320,i_Y_POS+heightt,0,0,col1,col1);//name[xpp]
    snwprintf(text,40,L"%d x %d",width,height);
    str=Str2ID(text,0,SID_ANY_LEN);
    DrawString(str,0, i_X_POS,1+i_Y_POS+heightt,320,i_Y_POS+heightt*2,0,0,col1,col1);
  }
  if (Counter && ilo)
  {
    snwprintf(text,40,L"%d/%d",xpp+1,ilo+1);
    str=Str2ID(text,0,SID_ANY_LEN);
    DrawString(str,0,c_X_POS,c_Y_POS,320,c_Y_POS+heightt*2,0,0,col1,col1);
  }
  GC_validate_RECT(gc,&rc_old);
  TextFree(str);
}

int BoardGuiOnCreate(DISP_OBJ_board *db)
{
  return (1);
}

void Auto(void)//orient display
{
  BOOK * bk=DISPLAY_GetTopBook(0);
  if(ori==0) BookObj_SetDisplayOrientation(bk, 0);
  else if(ori==1) BookObj_SetDisplayOrientation(bk, 1);
  center();
}

void zooom(int mode)
{
  int zxst=(width*100/(102-zoom)+1)/100, zyst=(height*100/(102-zoom)+1)/100;
  if(mode)
  {
    swidth=swidth+zxst;
    pozx-=zxst/2;
    sheight=sheight+zyst;
    pozy-=zyst/2;
  }else{
    swidth=swidth-zxst;
    pozx+=zxst/2;
    sheight=sheight-zyst;
    pozy+=zyst/2;
  }
  if (swidth<0) swidth=5;
  if (sheight<0) sheight=5;
}

void change(int mode) //prev/next image
{
  xpp+=mode;
  if (xpp>ilo) xpp=0;
  else if(xpp<0) xpp=ilo;
  wchar_t * nazwa=new wchar_t[128];
  TextID2wstr(((FILE*)ListElement_GetByIndex(MMSBook->flist,xpp))->strname,nazwa,SID_ANY_LEN);
  unreg();
  RegisterImage(MMSBook->path,nazwa);
  delete(nazwa);
  if(autoorient) Auto();
}

void BoardGuiOnKey(DISP_OBJ_board *db,int key,int,int repeat,int type)
{
  if(type==KBD_SHORT_PRESS || type==KBD_REPEAT)
  {
    if (key==LEFT_KEY)
    {
      if(ori && LEFT_KEY!=KEY_LEFT) pozy=pozy+10;//down
      else pozx=pozx-10;//left
    }
    if (key==RIGHT_KEY)
    {
      if(ori && RIGHT_KEY!=KEY_RIGHT) pozy=pozy-10;//up
      else pozx=pozx+10;//right
    }
    if (key==UP_KEY)
    {
      if(ori && UP_KEY!=KEY_UP) pozx=pozx-10;//left
      else pozy=pozy-10;//up
    }    
    if (key==DOWN_KEY)
    {
      if(ori && DOWN_KEY!=KEY_DOWN) pozx=pozx+10;//right
      else pozy=pozy+10;//down
    }
    if (key==ZOOMI_KEY)
    {
      zooom(1);
    }
    if (key==ZOOMD_KEY)
    {
      zooom(0);
    }
  }
    if (key==NEXT_KEY && type==NEXTKEYMODE)
    {
      change(1);
    }    
    if (key==PREV_KEY && type==PREVKEYMODE)
    {
      change(-1);
    }
    if (key==CENTER_KEY && type==CENTERKEYMODE)
    {
      center();
    }    
    if (key==ORIENT_KEY && type==ORIENTKEYMODE)
    {
      BOOK * bk=DISPLAY_GetTopBook(0);
      if(ori)
      {
        BookObj_SetDisplayOrientation(bk,ori=0);
      }else{
        BookObj_SetDisplayOrientation(bk,ori=1);
      }
      center();
      if(autostretch) stretch();
    }
    if (key==STR_KEY && type==STRKEYMODE)
    {
     stretch();
    }
    if (key==SI_KEY && type==SIKEYMODE)
    {
     info=!info;
    }
    if (key==RS_KEY && type==RSKEYMODE)
    {
     real_size();
    }
  InvalidateRect(&db->dsp_obj,0);
}

void OnLeftSoftkey(BOOK * bk, void *)
{
}

void OnRightSoftkey(BOOK * bk, void *)
{
 onBcfgConfig(0,0);
}

void BoardGui_constr(DISP_DESC *desc)
{
  DISP_DESC_SetName(desc,"Gui_IView");
  DISP_DESC_SetSize(desc,sizeof(DISP_OBJ_board));
  DISP_DESC_SetOnCreate(desc,(DISP_OBJ_ONCREATE_METHOD)BoardGuiOnCreate);
  DISP_DESC_SetOnRedraw(desc,(DISP_OBJ_ONREDRAW_METHOD)BoardGuiOnRedraw);
  DISP_DESC_SetOnKey(desc,(DISP_OBJ_ONKEY_METHOD)BoardGuiOnKey);
}

void BoardGui_destr(DISP_DESC *desc)
{
}

void OnCloseBoard(BOOK * bk, void *)
{
  TerminateElf(0,bk);
}

GUI_Board *CreateBoardGui(MyBOOK * MSBook)
{
  GUI_Board *gui_board=new GUI_Board;
  if (!CreateObject((GUI *)gui_board,BoardGui_destr,BoardGui_constr, &MSBook->book,0,0,0))
  {
    delete gui_board;
    return 0;
  }
  DISP_OBJ_board *disp_obj;
  disp_obj=(DISP_OBJ_board *)GUIObj_GetDISPObj(gui_board);
  disp_obj->mb=MSBook;
  MSBook->board=(GUI_type*)gui_board;
  if (MSBook) addGui2book(&MSBook->book,(GUI*)MSBook->board);
  GUI_SetStyle(MSBook->board,4); //                Fullscreen GUI. Don't change anyway!!
  GuiObject_SetTitleType((GUI*)MSBook->board, 1);
  GUIObject_HideSoftkeys(MSBook->board);
  GUIObject_Softkey_SetAction(MSBook->board,ACTION_BACK, OnCloseBoard);
  GUIObject_Softkey_SetAction(MSBook->board,ACTION_LONG_BACK, OnCloseBoard);
  //GUIObject_Softkey_SetAction(MSBook->board,ACTION_SELECT, OnLeftSoftkey);
  GUIObject_Softkey_SetAction(MSBook->board,ACTION_MORE, OnRightSoftkey);
  if(ori==1) BookObj_SetDisplayOrientation(&MSBook->book, 1);
  else BookObj_SetDisplayOrientation(&MSBook->book, 0);
  list();
  return gui_board;
}
